<?php
//刷还小额通道
namespace app\pay\service;
class Hqxe extends Hqbase
{
    protected $mchId = "jn2019032514062";//商户号
    protected $signKey = "baede106";//密钥
    protected $version = "0100";
    protected $apiUrl="http://pay.huanqiuhuiju.com/authsys/api/channel/pay/execute.do";


    public function index(){
        $data=[];
        $data["subMerchantNo"]="09b9d7924fbda0a99b7114df4dc487a2";
        $data["amount"]="1";
        $data["userFee"]="1";
        $data["bankcard"]="13271488080";
        $data["mobile"]="13271488080";
        $data["bindId"]="13271488080";
        $data["province"]="北京市";
        $data["deviceId"]=md5("09b9d7924fbda0a99b7114df4dc487a2");
        $data["deviceType"]=md5("09b9d7924fbda0a99b7114df4dc487a2");
        $data["userId"]=md5("09b9d7924fbda0a99b7114df4dc487a2");
        $this->pay($data);
    }


    /**
     * 鉴权
     * @param array $param
     * @return bool|mixed
     */
    public function register($param=[]){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data = [
            //公共参数
            "methodname"=>"register",//方法名
            "transcode"=>"050",//交易码
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            //业务参数
            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "bankcard"=>$param["bankcard"],//银行卡号
            "username"=>$param["username"],//真实姓名
            "idcard"=>$param["idcard"],//身份证号
            "mobile"=>$param["mobile"],//预留手机号
            "futureRateValue"=>$param["futureRateValue"],//扣款费率如：0.5 表示0.5%
            "fixAmount"=>$param["fixAmount"],//还款手续费以分为单位，不能有小数点
        ];
        $res = $this->unified($data);
        return $res;
    }


	/**
	 * 修改费率
	 * @return bool|mixed|string
	 */
    public function updateRage(){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data=[
            "methodname"=>"update",//方法名
            "transcode"=>"050",//交易码;037落地还款H;902查询接口
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "futureRateValue"=>$param["bankcard"],//扣款费率
            "fixAmount"=>$param["username"],//还款手续费
        ];
        return $this->unified($data);
    }


	/**
	 * 客户绑卡
	 * @param $param
	 *
	 * @return bool|mixed|string
	 */
    //$json_data = json_encode($data,JSON_UNESCAPED_UNICODE);
    public function bindCard($param){
        if(empty($param) || !is_array($param)){
            return false;
        }
        $data=[
            "methodname"=>"bindCard",//方法名
            "transcode"=>"050",//交易码;037落地还款H;902查询接口
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "bankcard"=>$param["bankcard"],//银行卡
            "username"=>$param["username"],//姓名
            "idcard"=>$param["idcard"],//身份证
            "mobile"=>$param["mobile"],//手机号
            "cardType"=>$param["cardType"],//卡类型借记卡：01 贷记卡：02
            "returnUrl"=>$param["returnUrl"],//前台跳转地址
            "notifyUrl"=>$param["notifyUrl"],//异步通知地址
        ];
        if($param["cardType"] == "02"){
        	$data["cvn2"] = $param["cvn2"];//CVN2
        	$data["expireDate"] = $param["expireDate"];//信用卡有效期
		}
        //halt($data);
        $res = $this->unified($data);
//        $res=[
//			"transcode" =>  "050",
//			"merchno" =>  "jn2019032514062",
//			"dsorderid" =>  "B19042409591060307042353",
//			"sign" =>  "3995afed76a6707817f45d849744d7ee",
//			"ordersn" =>  "B19042409591060307042353",
//			"returncode" =>  "0000",
//			"errtext" =>  "成功",
//			"bindId" =>  "BK6424262280",
//		];
		//halt($res);
        return $res;
    }



    //解绑卡;注意：解绑用户余额必须为零
    public function unBindCard(){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data=[
            "methodname"=>"unBindCard",//方法名
            "transcode"=>"037",//交易码;037落地还款H;902查询接口
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "bankcard"=>$param["bankcard"],//银行卡
            "bindId"=>$param["bindId"],//绑卡Id
            "returnUrl"=>$param["returnUrl"],//前台跳转地址
            "notifyUrl"=>$param["notifyUrl"],//异步通知地址
        ];
        return $this->unified($data);
    }
	//支付 扣钱
	//代付 还款
    //快捷支付 扣钱;
    public function pay($param){
        if(empty($param) || !is_array($param)){
            return false;
        }
        $data=[
            "methodname"=>"pay",//方法名
            "transcode"=>"050",//交易码;037落地还款H;902查询接口
            "version"=>"0100",//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "bindId"=>$param["bindId"],//绑卡ID
            "amount"=>$param["amount"],//金额以分为单位，不能有小数
            //"userFee"=>$param["userFee"],//交易手续费xuan
            "notifyUrl"=>$param["notifyUrl"],//异步通知地址
        ];
		if($param["userFee"]>=0 && isset($param["userFee"])){
			$data["userFee"] = $param['userFee'];
		}
        if(!empty($param["province"]) && isset($param["province"])){
        	$data["province"] = $param['province'];
		}
		if(!empty($param["mcc"]) && isset($param["mcc"])){//mcc行业类型码
			if(is_numeric($param["mcc"])){
				$data["mcc"] = $param['mcc'];
			}
		}
		//halt($data);
		$res = $this->unified($data);
        return $res;
    }


    //代付（还款）
    public function withDraw($param){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data=[
            "methodname"=>"withDraw",//方法名
            "transcode"=>"050",//交易码;037落地还款H;902查询接口
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号


            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "bindId"=>$param["bindId"],//绑卡ID

            "amount"=>$param["amount"],//金额
			"notifyUrl"=>$param["notifyUrl"]
        ];
        if(isset($param["userFee"]) && $param["userFee"]>0){
            $data["userFee"] = $param["userFee"];//交易手续费
        }
        //19050601484846765042359
        //halt($data);
        return $this->unified($data);
    }


    //商户余额接口
    public function queryBalance($param){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data=[
            "methodname"=>"queryBalance",//方法名
            "transcode"=>"050",//交易码;037落地还款H;902查询接口
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "subMerchantNo"=>$param["subMerchantNo"],//子商户号
            "bindId"=>$param["bindId"],//子商户号
        ];
        return $this->unified($data);
    }


    //订单查询接口
    public function queryOrder($param){
        if(empty($param) || !is_array($param)){
            return false;
        }

        $data=[
            "transcode"=>"902",//订单查询
            "version"=>$this->version,//版本号
            "merchno"=>$this->mchId,//商户号

            "ordersn"=>$param["ordersn"],//流水号
            "dsorderid"=>$param["dsorderid"],//商户订单号
            "transtype"=>$param["transtype"],//交易类型133交易，134代付
        ];
		//halt($data);
        return $this->unified($data,"http://pay.huanqiuhuiju.com/authsys/api/auth/execute.do");
    }






    private function create_order_no() {
        $order_no = date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
        return $order_no;
    }

}